﻿using System;
using System.Windows;
using SystemX.Web;
using System.Collections.Generic;

namespace SystemX.Infrastructure
{
	public static partial class AppSettingsHelper
	{
		public static Dictionary<string, string> AppSettings
		{
			get
			{
				if (Application.Current.Resources.Count == 0) return null;
				return Application.Current.Resources.GetByKey("AppSettings") as Dictionary<string, string>;
			}
		}
		public static string GetSetting(string key)
		{
			if (AppSettings == null || (!AppSettings.ContainsKey(key)))
			{
				return null;
			}
			return AppSettings[key];
		}
		public static int GetAsInt(string key) 
		{
			var val = GetSetting(key);
			if (string.IsNullOrEmpty(val)) return int.MinValue;
			return Convert.ToInt32(val);
		}
		public static double GetAsDouble(string key) {
			var val = GetSetting(key);
			if (string.IsNullOrEmpty(val)) return double.NaN;
			return Convert.ToDouble(val);
		}
		public static bool GetAsBool(string key) {
			var val = GetSetting(key);
			if (string.IsNullOrEmpty(val)) return false;
			var retVal = default(bool);
			bool.TryParse(val, out retVal);
			return retVal;
		}
	}
}
